package com.zx.task.manager.controller;

import com.zx.core.wrapper.ObjectWrapper;
import com.zx.core.wrapper.PageWrapper;
import com.zx.core.wrapper.ResponseWrapper;
import com.zx.log.annotation.OperateLog;
import com.zx.task.manager.param.TasksParam;
import com.zx.task.manager.service.ITasksService;
import com.zx.task.manager.vo.TasksVO;
import com.zx.validation.annotation.ParameterValidation;
import com.zx.validation.annotation.ParameterValidations;
import com.zx.validation.enums.ValidateRulesEnum;
import com.zx.validation.groups.Create;
import com.zx.validation.groups.Query;
import com.zx.validation.groups.Update;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * description: 任务控制器 <br>
 * create: 2025-10-13 20:15
 * </p>
 *
 * @author zhou  xun
 */
@RestController
@RequestMapping("/tasks")
@RequiredArgsConstructor
public class TasksController {

    private final ITasksService iTasksService;

    /**
     * 分页查询任务列表
     *
     * @param tasksParam 任务查询参数
     * @return 任务列表
     */
    @PreAuthorize("hasAuthority('task:list')")
    @GetMapping("/list")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "任务参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Query.class})
    )
    public PageWrapper<TasksVO> list(TasksParam tasksParam) {
        return iTasksService.getTasksVOListByPage(tasksParam);
    }

    /**
     * 新增任务
     *
     * @param tasksParam 任务参数
     * @return {@link ResponseWrapper}
     */
    @OperateLog(value = "新增任务")
    @PreAuthorize("hasAuthority('task:add')")
    @PostMapping("/addTask")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "任务参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Create.class})
    )
    public ResponseWrapper addTask(@RequestBody TasksParam tasksParam) {
        return iTasksService.addTask(tasksParam);
    }

    /**
     * 编辑任务
     *
     * @param tasksParam 任务参数
     * @return {@link ResponseWrapper}
     */
    @OperateLog(value = "编辑任务")
    @PreAuthorize("hasAuthority('task:edit')")
    @PutMapping("/editTask")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "任务参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Update.class})
    )
    public ResponseWrapper editTask(@RequestBody TasksParam tasksParam) {
        return iTasksService.editTask(tasksParam);
    }

    /**
     * 删除任务
     *
     * @param taskId 任务Id
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2025-10-13
     */
    @OperateLog(value = "删除任务")
    @PreAuthorize("hasAuthority('task:remove')")
    @DeleteMapping("/deleteTask/{taskId}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "任务Id", rules = {ValidateRulesEnum.NOT_NULL})
    })
    public ResponseWrapper deleteTask(@PathVariable("taskId") String taskId) {
        return iTasksService.deleteTask(taskId);
    }

    /**
     * 获取指定任务信息
     *
     * @param taskId 任务Id
     * @return 任务信息
     */
    @OperateLog(value = "获取指定任务信息")
    @PreAuthorize("hasAuthority('task:detail')")
    @GetMapping("/info/{taskId}")
    public ObjectWrapper<TasksVO> getTaskInfo(@PathVariable("taskId") String taskId) {
        return iTasksService.selectTaskById(taskId);
    }

    /**
     * 发布任务
     *
     * @param tasksParam 任务参数
     * @return {@link ResponseWrapper}
     */
    @OperateLog(value = "发布任务")
    @PreAuthorize("hasAuthority('task:publish')")
    @PutMapping("/publishTask")
    public ResponseWrapper publishTask(@RequestBody TasksParam tasksParam) {
        return iTasksService.publishTask(tasksParam);
    }

    /**
     * 签收任务
     *
     * @param tasksParam 任务参数
     * @return {@link ResponseWrapper}
     */
    @OperateLog(value = "签收任务")
    @PreAuthorize("hasAuthority('task:claim')")
    @PutMapping("/claimTask")
    public ResponseWrapper claimTask(@RequestBody TasksParam tasksParam) {
        return iTasksService.claimTask(tasksParam);
    }


    /**
     * 完成任务
     *
     * @param tasksParam 任务参数
     * @return {@link ResponseWrapper}
     */
    @OperateLog(value = "完成任务")
    @PreAuthorize("hasAuthority('task:complete')")
    @PutMapping("/completeTask")
    public ResponseWrapper completeTask(@RequestBody TasksParam tasksParam) {
        return iTasksService.completeTask(tasksParam);
    }

    /**
     * 关闭任务
     *
     * @param tasksParam 任务参数
     * @return {@link ResponseWrapper}
     */
    @OperateLog(value = "关闭任务")
    @PreAuthorize("hasAuthority('task:close')")
    @PutMapping("/closeTask")
    public ResponseWrapper closeTask(@RequestBody TasksParam tasksParam) {
        return iTasksService.closeTask(tasksParam);
    }

    /**
     * 取消任务
     *
     * @param tasksParam 任务参数
     * @return {@link ResponseWrapper}
     */
    @OperateLog(value = "取消任务")
    @PreAuthorize("hasAuthority('task:cancel')")
    @PutMapping("/cancelTask")
    public ResponseWrapper cancelTask(@RequestBody TasksParam tasksParam) {
        return iTasksService.cancelTask(tasksParam);
    }

    /**
     * 更新任务进度
     *
     * @param tasksParam 任务参数
     * @return {@link ResponseWrapper}
     */
    @OperateLog(value = "更新任务进度")
    @PreAuthorize("hasAuthority('task:edit')")
    @PutMapping("/updateProgress")
    public ResponseWrapper updateProgress(@RequestBody TasksParam tasksParam) {
        return iTasksService.updateProgress(tasksParam);
    }

    /**
     * 转派任务
     *
     * @param tasksParam 任务参数
     * @return {@link ResponseWrapper}
     */
    @OperateLog(value = "转派任务")
    @PreAuthorize("hasAuthority('task:edit')")
    @PutMapping("/reassignTask")
    public ResponseWrapper reassignTask(@RequestBody TasksParam tasksParam) {
        return iTasksService.reassignTask(tasksParam);
    }

}